﻿using LiveCharts;
using LiveCharts.Defaults;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhaoxi.Industrial.Base;

namespace Zhaoxi.Industrial.Model
{
    public class MonitorValuesModel:NotifyPropertyBase
    {
        public Action<MonitorValueState, string,string> ValueStateChanged;
        public string ?id { get; set; }
        public string ?value_id { get; set; }
        public string ?value_name { get; set; }
        public string storage_area_id { get; set; }
        public int address { get; set; }
        public string ?datatype { get; set; }
        public bool isAlarm { get; set; }
        public string ?unit { get; set; }
        public double alarm_lolo { get; set; }
        public double alarm_low { get; set; }
        public double alarm_high { get; set; }
        public double alarm_hihi { get; set; }
        public string ?Valuedes { get; set; }

        private double _content;
        
        public double content {
            get { return _content; }
            set {
                _content = value;
                if (isAlarm)
                {
                    string msg = Valuedes;
                    MonitorValueState state = new MonitorValueState();
                    if (value < alarm_lolo)
                    {
                        msg = "极低";
                        state = MonitorValueState.LL;
                    }
                    else if (value < alarm_low)
                    {
                        msg = "较低";
                        state = MonitorValueState.LOW;
                    }
                    else if (value > alarm_high)
                    {
                        msg = "较高";
                        state = MonitorValueState.HIGH;
                    }
                    else if(value > alarm_hihi)
                    {
                        msg = "超高";
                        state = MonitorValueState.HH;
                    }
                    ValueStateChanged(state, msg + "。当前值：" + value.ToString(),id);
                    
                }
                Values.Add(new ObservableValue(value));

                if (Values.Count > 60) Values.RemoveAt(0);
                this.RaisePropertyChanged();
            }
        }

        // 图表集合，如果数值发生改变会自动修改，ChartValues 的属性
        public ChartValues<ObservableValue> Values { get; set; } = new ChartValues<ObservableValue>();
    }
}
